#include "../curl/CurlClient.h"
#include "../http/HttpData.h"
#include "../common/global.h"
#include "../common/CCReactor.h"
#include <iostream>
#include <thread>
#include <chrono>
#include <vector>
#include <atomic>
#include <memory>

class SimplePlayerSimulator {
public:
    SimplePlayerSimulator(int player_id, const std::string& server_url)
        : player_id_(player_id), server_url_(server_url), running_(false) {}
    
    void start() {
        running_ = true;
        thread_ = std::thread(&SimplePlayerSimulator::run, this);
    }
    
    void stop() {
        running_ = false;
        if (thread_.joinable()) {
            thread_.join();
        }
    }
    
private:
    void run() {
        std::cout << "Player " << player_id_ << " starting..." << std::endl;
        
        // Just make one simple request
        if (running_) {
            send_request();
        }
        
        std::cout << "Player " << player_id_ << " finished" << std::endl;
    }
    
    void send_request() {
        std::cout << "Player " << player_id_ << " sending request..." << std::endl;
        
        try {
            CurlClient client;
            if (!client.Initialize()) {
                std::cout << "Player " << player_id_ << " failed to initialize client" << std::endl;
                return;
            }
            
            stHttpRequest req;
            req.method = "GET";
            req.url = server_url_ + "/player_action";
            req.heads["User-Agent"] = "SimplePlayerSimulator/1.0";
            req.heads["X-Player-Id"] = std::to_string(player_id_);
            req.heads["X-Request-Id"] = std::to_string(1);
            
            stHttpResponse resp;
            int result = client.Request(std::move(req), resp);
            
            std::cout << "Player " << player_id_ << " request completed: " << result << std::endl;
            
        } catch (const std::exception& e) {
            std::cout << "Player " << player_id_ << " exception: " << e.what() << std::endl;
        }
    }
    
    int player_id_;
    std::string server_url_;
    std::atomic<bool> running_;
    std::thread thread_;
};

int main() {
    std::cout << "=== Simple Player Test ===" << std::endl;
    
    try {
        // Initialize reactor
        g_reactor->Init();
        g_reactor->Start();
        
        std::cout << "Reactor initialized" << std::endl;
        
        // Test with simple players
        const int num_players = 3;
        std::vector<std::unique_ptr<SimplePlayerSimulator>> players;
        std::string server_url = "http://127.0.0.1:8083";
        
        std::cout << "Creating " << num_players << " players..." << std::endl;
        
        for (int i = 0; i < num_players; i++) {
            players.emplace_back(new SimplePlayerSimulator(i + 1, server_url));
        }
        
        std::cout << "Starting players..." << std::endl;
        
        for (auto& player : players) {
            player->start();
            std::this_thread::sleep_for(std::chrono::milliseconds(100));
        }
        
        std::cout << "Waiting for players to complete..." << std::endl;
        
        for (auto& player : players) {
            player->stop();
        }
        
        std::cout << "All players completed" << std::endl;
        
        g_reactor->Stop();
        
    } catch (const std::exception& e) {
        std::cout << "Test failed with exception: " << e.what() << std::endl;
        return 1;
    }
    
    return 0;
}